package kkWindows;

 import javax.swing.*;
   import java.beans.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.io.*;
   import java.util.LinkedList;
   import javax.swing.table.*;

   public class accesoryPanel extends JPanel
           implements PropertyChangeListener {
     private static final int PREFERRED_WIDTH = 125;
     private static final int PREFERRED_HEIGHT = 10;
     JFileChooser filesChooser;
     JButton buttonAddFilesOnly;
     JButton buttonAddFiles;
     LinkedList filesList;
     JTable filesTable;
     
   class buttonListener implements ActionListener
   {
      
       buttonListener(){
           
       }
       public void actionPerformed(ActionEvent event)
       {
           String name=((JButton)(event.getSource())).getName();
           File selectedFiles[]=filesChooser.getSelectedFiles(),folderChecker;
           int i,j;
           filesList=new LinkedList();
           if(selectedFiles.length!=0)  {
                if(name.equals("AddFiles")){
                    for(i=0;i<selectedFiles.length ;i++) {
                        System.out.println(i+" : "+selectedFiles[i]+"\n");
                        filesList.addLast(selectedFiles[i].getAbsolutePath());
                        selectedFiles[i]=null;
                    }            
                }
                else if(name.equals("AddFilesOnly")){
                    
                    for(i=0;i<selectedFiles.length;i++){
                        if(selectedFiles[i].isDirectory()){
                            File[] folderFiles=selectedFiles[i].listFiles();
                            for(j=0;j<folderFiles.length;j++){
                                if(folderFiles[j].isFile()){
                                    filesList.addLast(folderFiles[j].getAbsolutePath());
                                }
                            }
                        }
                        //else
                          //addToTable(selectedFiles[i]);
                        else
                            filesList.addLast(selectedFiles[i].getAbsolutePath());
                        selectedFiles[i]=null;
                    }
                }
                addToTable();
           }
       }
       
       private void addToTable(){
           //filesList.addLast(item.getAbsolutePath());
           //commonTableModel model=new commonTableModel( filesList,new Object[] {"Serial","","List Of Files"},commonTableModel.FILES_TABLE );
           commonTableModel model;
           if(filesTable.getModel() instanceof commonTableModel){
               model= (commonTableModel)(filesTable.getModel());
               model.setData(filesList);
           }
           else
               model=new commonTableModel( filesList,new Object[] {"Serial","","List Of Files"},commonTableModel.FILES_TABLE );
           
           filesTable.setModel(model);
           kkScanWindow.initFilesTableProperties(filesTable);
           filesTable.repaint();
           
       }
   }

     public accesoryPanel(JFileChooser filesChooser,LinkedList filesList,JTable filesTable) {
        this.filesChooser=filesChooser;
        this.filesList=filesList;
        this.filesTable=filesTable;        
        
        SpringLayout layout=new SpringLayout();
        this.setLayout(layout);
        
        filesChooser.setMultiSelectionEnabled(true);
        filesChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
                      
        buttonAddFiles=new JButton("+ File(s)");
        buttonAddFiles.setName("AddFiles");
        buttonAddFiles.setToolTipText("Adds the selected files and folders to the list of files to be scanned");
        buttonAddFiles.setPreferredSize(new Dimension(80,20));
        buttonAddFiles.addActionListener(new buttonListener());
        
        buttonAddFilesOnly=new JButton("+ Files Only");
        buttonAddFilesOnly.setName("AddFilesOnly");
        buttonAddFilesOnly.setToolTipText("Adds the selected files and folders(excluding sub-folders) to the list of files to be scanned");
        buttonAddFilesOnly.setPreferredSize(new Dimension(100,20));
        buttonAddFilesOnly.addActionListener(new buttonListener());
        
        layout.putConstraint(SpringLayout.WEST, buttonAddFiles,
                     10,
                     SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, buttonAddFiles,
                     5,
                     SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.WEST, buttonAddFilesOnly,
                     10,
                     SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, buttonAddFilesOnly,
                     40,
                     SpringLayout.NORTH, this);
        
        add(buttonAddFiles);
        add(buttonAddFilesOnly);
        
        filesChooser.addPropertyChangeListener(this);
        setPreferredSize(new Dimension
               (PREFERRED_WIDTH, PREFERRED_HEIGHT));
     }   
     public void propertyChange(PropertyChangeEvent changeEvent) {
        String changeName = 
          changeEvent.getPropertyName();
        
        
          
     }
     
 }